package cn.JHai.rbac.controller;

import cn.JHai.rbac.RequiredPermission;
import cn.JHai.rbac.comm.CurrentUtil;

import cn.JHai.rbac.domain.Customertransfer;
import cn.JHai.rbac.domain.Employee;
import cn.JHai.rbac.service.CustomerService;
import cn.JHai.rbac.service.CustomerTransferService;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import org.springframework.ui.Model;

@Controller
@RequestMapping("customerTransfer")
public class CustomerTransferController {


    @Autowired
    private CustomerTransferService customerTransferService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiredPermission({"客户移交添加/更新","customerTransfer:saveOrUpdate"})
    public JSONObject saveOrUpdate(Customertransfer transfer, Long customer_id){
        System.out.println(transfer);
        customerTransferService.saveOrUpdata(transfer,customer_id);
        customerService.ChangeSeller(customer_id,transfer.getNew_seller_id());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",true);
        return jsonObject;
    }

    @RequestMapping("/absorb")
    @ResponseBody
    @RequiredPermission({"客户吸纳","customerTransfer:absorb"})
    public JSONObject absorb(Customertransfer transfer,Long customer_id) {
        System.out.println(customer_id);
        System.out.println(transfer);
        customerService.ChangeStaus(customer_id, 0l);
        JSONObject jsonObject = new JSONObject();
        Employee current = CurrentUtil.getCurrent();
        transfer.setNew_seller_id(current.getDept_id());
        customerTransferService.saveOrUpdata(transfer, customer_id);
        jsonObject.put("success", true);
        return jsonObject;
    }



    @RequestMapping("/list")
    @RequiredPermission({"客户移交列表","customerTransfer:list"})
    public String list(QueryObject queryObject , Model model){
//        System.out.println("****************************");
//        System.out.println(queryObject.getKeyword());
//        System.out.println("****************************");
        PageResult result = customerTransferService.query(queryObject);
        result.setCurrentKeyword(queryObject.getKeyword());
        model.addAttribute("result",result);
        return "/customerTransfer/list";
    }
}
